﻿using AuthTemplate.DTO.StudentCash;
using AuthTemplate.DTO.User;
using AuthTemplate.Entity;
using AuthTemplate.Infrastructure;
using AuthTemplate.Repository.Interface;
using AuthTemplate.Repository.Interface.IEntityRepository;
using AuthTemplate.Repository.Repositories;
using AuthTemplate.ViewModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AuthTemplate.Service
{
    public class StudentInfoService : BaseService, IStudentInfoService
    {
        private readonly IStudentInfoRepository studentinfoRepository;
        private readonly IUnitOfWork unitOfWork;
        public StudentInfoService(IStudentInfoRepository studentinfoRepository,
            IUnitOfWork unitOfWork)
        {
            this.studentinfoRepository = studentinfoRepository;
            this.unitOfWork = unitOfWork;
        }

        public void UploadStudent(List<string> paths)
        {
            foreach (string path in paths)
            {
                NpioExcelHelper helper = new NpioExcelHelper(path);
                Dictionary<int, string> sheets = helper.ReturnSheetList();
                List<string> oldStudentNo = new List<string>();//已处理过的学号集合，每个学号仅仅处理一次，再次发现则跳过
                foreach (var sheetName in sheets)
                {
                    DataTable dt = helper.ExcelToDataTable(sheetName.Value,true);
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            StudentInfo entity = new StudentInfo();
                            entity.StudentNo = dr["学号"].ToString();
                            entity.Name = dr["姓名"].ToString();
                            entity.IdCard = dr["证件号码"].ToString();

                            if (!oldStudentNo.Contains(entity.StudentNo))
                            {
                                List<string> oldentity = studentinfoRepository.FindBy(s => s.StudentNo == entity.StudentNo).Select(s => s.Id).ToList();
                                if (oldentity != null && oldentity.Count > 0)
                                {
                                    entity.Id = oldentity[0];
                                    studentinfoRepository.Edit(entity);
                                }
                                else
                                {
                                    studentinfoRepository.Add(entity);
                                }

                                oldStudentNo.Add(entity.StudentNo);
                            }

                        }
                        unitOfWork.Commit();
                    }
                }
            }
            
        }

        public void DeleteAllStudentInfo()
        {
            if(studentinfoRepository.GetAll().Count()>0)
            {
                string sql = @" delete from StudentInfo";
                studentinfoRepository.ExecuteSqlCommand(sql,null);
            }
        }
    }

    public interface IStudentInfoService
    {
        void UploadStudent(List<string> paths);
        void DeleteAllStudentInfo();
    }
}
